package com.huawei.hms.framework.network.restclient.hwhttp;

import com.huawei.hms.framework.common.CheckParamUtils;
import com.huawei.hms.framework.network.restclient.hwhttp.trans.ByteString;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import javax.annotation.Nullable;

public abstract class RequestBody {
    public static final String DEFAULT_CONTENT_TYPE = "text/plain; charset=UTF-8";
    public static final String HEAD_VALUE_CONTENT_TYPE_FORM_DATA = "multipart/form-data";
    public static final String HEAD_VALUE_CONTENT_TYPE_URLENCODED = "application/x-www-form-urlencoded";
    private static final Charset UTF_8 = Charset.forName("UTF-8");

    public abstract byte[] body();

    public abstract String contentType();

    public abstract void writeTo(OutputStream outputStream) throws IOException;

    public static RequestBody create(@Nullable final String str, final byte[] bArr) {
        if (bArr != null) {
            return new RequestBody() {
                @Nullable
                public String contentType() {
                    return str == null ? RequestBody.DEFAULT_CONTENT_TYPE : str;
                }

                public void writeTo(OutputStream outputStream) {
                }

                public byte[] body() {
                    return bArr;
                }
            };
        }
        throw new NullPointerException("content == null");
    }

    public static RequestBody create(String str) {
        if (str != null) {
            return new 2(str);
        }
        throw new NullPointerException("content == null");
    }

    public static RequestBody create(@Nullable MediaType mediaType, String str) {
        if (str == null) {
            throw new NullPointerException("content == null");
        }
        Charset charset = UTF_8;
        if (mediaType != null) {
            charset = mediaType.charset();
            if (charset == null) {
                charset = UTF_8;
                mediaType = MediaType.parse(mediaType + "; charset=utf-8");
            }
        }
        byte[] bytes = str.getBytes(charset);
        return create(mediaType, bytes, 0, bytes.length);
    }

    public static RequestBody create(@Nullable MediaType mediaType, byte[] bArr, int i, int i2) {
        if (bArr == null) {
            throw new NullPointerException("content == null");
        }
        CheckParamUtils.checkOffsetAndCount((long) bArr.length, (long) i, (long) i2);
        return new 3(mediaType, i2, bArr, i);
    }

    public static RequestBody create(@Nullable MediaType mediaType, ByteString byteString) {
        return new 4(mediaType, byteString);
    }

    public static RequestBody create(@Nullable MediaType mediaType, File file) {
        if (file != null) {
            return new 5(mediaType, file);
        }
        throw new NullPointerException("content == null");
    }

    public long contentLength() throws IOException {
        byte[] body = body();
        if (body != null) {
            return (long) body.length;
        }
        return 0;
    }
}
